package pab.ui;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.*;

import pab.ui.auxiliary.Base64;
/*
 * Created by JFormDesigner on Tue Jun 07 09:29:54 ICT 2011
 */



/**
 * @author rukowen
 */
public class PasswordChange extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String propPath;
	public PasswordChange(String propPath) {
		initComponents();
		this.propPath = propPath;
		this.setModal(true);
	}

	private void btnOkMouseClicked(MouseEvent e) {
		// TODO add your code here
		@SuppressWarnings("deprecation")
		String oldPass = txtPassword.getText();
		@SuppressWarnings("deprecation")
		String newPass = txtNewPass.getText();
		@SuppressWarnings("deprecation")
		String confirm = txtConfirm.getText();
		
		
		try {
			Properties prop = new Properties();
			FileInputStream fis = new FileInputStream(new File(propPath));;
			try {
				prop.load(fis);
				if(Base64.encode(oldPass).equals(prop.getProperty("user"))){
					if(newPass.equals(confirm)){
						fis.close();
						FileOutputStream fos = new FileOutputStream(propPath);
						prop.setProperty("user", Base64.encode(newPass));  
						prop.store(fos, "null");
						fos.close();
						this.dispose();
					}else{
						JOptionPane.showMessageDialog(this, "Invalid confirm password");
					}

				}else{
					JOptionPane.showMessageDialog(this, "Invalid old password");
				}
					
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label1 = new JLabel();
		txtPassword = new JPasswordField();
		btnOk = new JButton();
		label2 = new JLabel();
		txtNewPass = new JPasswordField();
		label3 = new JLabel();
		txtConfirm = new JPasswordField();

		//======== this ========
		setTitle("Password Change");
		setResizable(false);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Old Password");
		contentPane.add(label1);
		label1.setBounds(55, 40, 86, 16);
		contentPane.add(txtPassword);
		txtPassword.setBounds(155, 30, 190, 28);

		//---- btnOk ----
		btnOk.setText("Ok");
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnOkMouseClicked(e);
			}
		});
		contentPane.add(btnOk);
		btnOk.setBounds(155, 125, 80, 28);

		//---- label2 ----
		label2.setText("New Password");
		contentPane.add(label2);
		label2.setBounds(55, 70, 86, 16);
		contentPane.add(txtNewPass);
		txtNewPass.setBounds(155, 60, 190, 28);

		//---- label3 ----
		label3.setText("Confirm");
		contentPane.add(label3);
		label3.setBounds(55, 95, 86, 16);
		contentPane.add(txtConfirm);
		txtConfirm.setBounds(155, 90, 190, 28);

		contentPane.setPreferredSize(new Dimension(400, 195));
		setSize(400, 195);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label1;
	private JPasswordField txtPassword;
	private JButton btnOk;
	private JLabel label2;
	private JPasswordField txtNewPass;
	private JLabel label3;
	private JPasswordField txtConfirm;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
